package leetcode_100;

public class CountAndSay_38 {

	public static void main(String[] args) {
		System.out.println(countAndSay(5));

	}
	
	 public static String countAndSay(int n) {
		 StringBuilder res=new StringBuilder("1");
		 //开始计数并保存当前的字符串结果
	     if(n==1)
	    	 return res.toString();
	     else {
	    	 int i=2;
	    	 while(i<=n) {
	    		 StringBuilder now=new StringBuilder();
	    		 char nowChar=res.charAt(0);
	    		 int count=0;
	    		 for(int j=0;j<res.length();++j) {//遍历上一个字符串结果
	    			 if(res.charAt(j)!=nowChar) {
	    				 now.append(String.valueOf(count)+nowChar);
	    				 nowChar=res.charAt(j);
	    				 count=1;
	    			 }
	    			 else
	    				 ++count;
	    		 }
	    		 now.append(String.valueOf(count)+nowChar);
	    		 res=now;
	    		 ++i;
	    	 }
	     }
	     return res.toString();
	 }

}
